package com.heima.wemedia.service.impl;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.api.feign.ApArticleFeignClient;
import com.heima.audit.baidu.BaiduImageScan;
import com.heima.audit.baidu.BaiduTextScan;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {
    @Autowired
    private WmNewsMapper wmNewsMapper;
    @Autowired
    private ApArticleFeignClient apArticleFeignClient;
    @Autowired
    private BaiduImageScan baiduImageScan;
    @Autowired
    private BaiduTextScan baiduTextScan;

    @Override
    @Async
    public void informationReview(WmNews wmNews) {
        String content = wmNews.getContent();
        Integer conclusionType = baiduTextScan.textScan(content);
        Date publishTime = wmNews.getPublishTime();
        Date createdTime = wmNews.getCreatedTime();
        if (conclusionType == 1) {
            if (publishTime.after(createdTime)) {
                LambdaUpdateWrapper<WmNews> updateWrapper = Wrappers.<WmNews>lambdaUpdate().set(WmNews::getStatus, 8).eq(WmNews::getId, wmNews.getId());
                wmNewsMapper.update(null, updateWrapper);
            } else if (createdTime.after(publishTime)) {
                LambdaUpdateWrapper<WmNews> updateWrapper = Wrappers.<WmNews>lambdaUpdate().set(WmNews::getStatus, 9).eq(WmNews::getId, wmNews.getId());
                wmNewsMapper.update(null, updateWrapper);
            }
        } else if (conclusionType == 2 || conclusionType == 3 || conclusionType == 4) {
            LambdaUpdateWrapper<WmNews> updateWrapper = Wrappers.<WmNews>lambdaUpdate().set(WmNews::getStatus, 2).eq(WmNews::getId, wmNews.getId());
            wmNewsMapper.update(null, updateWrapper);
        }
    }

    @Scheduled(cron = "0 * * * * ?")
    @Async
    public void automaticReview() {
        List<WmNews> wmNews = wmNewsMapper.selectList(null);
        List<String> totalImagePath = new ArrayList<>();
        for (WmNews wmNew : wmNews) {
            List<String> totalTextPath = new ArrayList<>();
//            Integer imageSuccess = null;
            String imagePath = null;
            String textPath = null;
            // 将JSON字符串转换为JSONArray
            JSONArray jsonArray = new JSONArray(wmNew.getContent());
            // 遍历JSONArray
            for (int i = 0; i < jsonArray.length(); i++) {
                // 获取每一个JSONObject
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                // 检查"type"是否为"image"
                if ("image".equals(jsonObject.getString("type"))) {
                    // 获取"value"字段的值（即图片路径）
                    imagePath = jsonObject.getString("value");
                    totalImagePath.add(imagePath);
                }
                if ("text".equals(jsonObject.getString("type"))) {
                    textPath = jsonObject.getString("value");
                    totalTextPath.add(textPath);
                }

            }
            String totalPath = totalTextPath.toString();
            Integer textSuccess = baiduTextScan.textScan(totalPath);
            totalTextPath.clear();
            try {
                if (textSuccess == 1) {
                    if (wmNew.getPublishTime().after(new Date())) {
                        LambdaUpdateWrapper<WmNews> updateWrapper = Wrappers.<WmNews>lambdaUpdate().set(WmNews::getStatus, 8).eq(WmNews::getId,wmNew.getId());
                        wmNewsMapper.update(null, updateWrapper);
                    }
                    if (new Date().after(wmNew.getPublishTime())) {
                        LambdaUpdateWrapper<WmNews> updateWrapper = Wrappers.<WmNews>lambdaUpdate().set(WmNews::getStatus, 9).eq(WmNews::getId,wmNew.getId());
                        wmNewsMapper.update(null, updateWrapper);
                        ArticleDto articleDto = new ArticleDto();
                        articleDto.setAuthorId(Long.valueOf(wmNew.getUserId()));
                        articleDto.setContent(textPath);
                        articleDto.setTitle(wmNew.getTitle());
                        articleDto.setAuthorName("admin");
                        articleDto.setChannelId(wmNew.getChannelId());
                        articleDto.setLayout(wmNew.getType());
                        articleDto.setImages(imagePath);
                        articleDto.setLabels(wmNew.getLabels());
                        articleDto.setLikes(0);
                        articleDto.setCollection(0);
                        articleDto.setComment(0);
                        articleDto.setViews(0);
                        articleDto.setCreatedTime(wmNew.getPublishTime());
                        articleDto.setPublishTime(wmNew.getPublishTime());
                        articleDto.setSyncStatus(true);
                        articleDto.setOrigin(true);
                        apArticleFeignClient.save(articleDto);
                    }
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }

        }
    }
}